/**
 * CustomerVideoView.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-6-20 下午2:20:57
 */
package com.ghyf.mplay.view;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;

/**
 *  
 * @author cuiran
 * @version 1.0.0
 */
public class CustomerVideoView extends SurfaceView implements MediaPlayerControl {
	private static String TAG = "CustomerVideoView"; 
	
	//class property
	private int module_type; // 模块类型
	private int zOrder; // 模块的ZOrder
	private String file_version;	//文件版本
	private String module_name;	//控件名
	private int module_uid;//控件标识
	private int module_gid;//控件组标识
	
	//player property
    private boolean pause; 
    private boolean seekBackward; 
    private boolean seekForward; 
    private Uri videoUri; 
    private MediaPlayer mediaPlayer; 
    private Context context; 
    private OnPreparedListener onPreparedListener; 
    private int videoWidth; 
    private int videoHeight; 
    private MediaController mediaController; 
    protected SurfaceHolder surfaceHolder; 
	
    private Callback surfaceHolderCallback = new SurfaceHolder.Callback() { 
        public void surfaceChanged(SurfaceHolder holder, int format, int w, 
                int h) { 
        } 
        public void surfaceCreated(SurfaceHolder holder) { 
            surfaceHolder = holder; 
            if (mediaPlayer != null) { 
                mediaPlayer.setDisplay(surfaceHolder); 
                resume(); 
            } else { 
                openVideo(); 
            } 
        } 
        public void surfaceDestroyed(SurfaceHolder holder) { 
            surfaceHolder = null; 
            if (mediaController != null) { 
                mediaController.hide(); 
            } 
            release(true); 
        } 
    }; 
    
    public void stopPlayback() {
	    if (mediaPlayer != null) {
	    	mediaPlayer.stop();
	    	mediaPlayer.release();
	    	mediaPlayer = null;
	   
	    }
	  }
    private void release(boolean cleartargetstate) { 
        if (mediaPlayer != null) { 
            mediaPlayer.reset(); 
            mediaPlayer.release(); 
            mediaPlayer = null; 
        } 
    } 
    public void resume() { 
        if (surfaceHolder == null) { 
            return; 
        } 
        if (mediaPlayer != null) { 
            return; 
        } 
        openVideo(); 
    } 
    public CustomerVideoView(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
        this.context = context; 
        this.initVideoView(); 
    } 
    public CustomerVideoView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        this.context = context; 
        this.initVideoView(); 
    } 
    public CustomerVideoView(Context context) { 
        super(context); 
        this.context = context; 
        this.initVideoView(); 
    } 
    @Override 
    public boolean canPause() { 
        return this.pause; 
    } 
    @Override 
    public boolean canSeekBackward() { 
        return this.seekBackward; 
    } 
    @Override 
    public boolean canSeekForward() { 
        return this.seekForward; 
    } 
    @Override 
    public int getBufferPercentage() { 
        return 0; 
    } 
    @Override 
    public int getCurrentPosition() { 
        return mediaPlayer!=null?mediaPlayer.getCurrentPosition():0; 
    } 
    @Override 
    public int getDuration() { 
        return mediaPlayer!=null?mediaPlayer.getDuration():0; 
    } 
    @Override 
    public boolean isPlaying() { 
        return false; 
    } 
    @Override 
    public void pause() { 
    } 
    @Override 
    public void seekTo(int mSec) { 
    } 
    @Override 
    public void start() { 
    } 
    public void setVideoURI(Uri uri) { 
        this.videoUri = uri; 
        openVideo(); 
        requestLayout(); 
        invalidate(); 
    } 
    private void openVideo() { 
        this.mediaPlayer = new MediaPlayer(); 
        try { 
            this.mediaPlayer.setDataSource(this.context, this.videoUri); 
        } catch (Exception e) { 
            Log.e(TAG, e.getMessage()); 
            throw new RuntimeException(e); 
        } 
        this.mediaPlayer.prepareAsync(); 
        this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
        this.mediaPlayer.setOnPreparedListener(onPreparedListener); 
        attachMediaController(); 
    } 
    private void attachMediaController() { 
        if (mediaPlayer != null && mediaController != null) { 
            mediaController.setMediaPlayer(this); 
            View anchorView = this.getParent() instanceof View ? (View) this 
                    .getParent() : this; 
            mediaController.setAnchorView(anchorView); 
            mediaController.setEnabled(true); 
        } 
    } 
    public void setMediaController(MediaController controller) { 
        if (mediaController != null) { 
            mediaController.hide(); 
        } 
        mediaController = controller; 
        attachMediaController(); 
    } 
    public void setOnPreparedListener(OnPreparedListener onPreparedListener) { 
        this.onPreparedListener = onPreparedListener; 
    } 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
        int width = getDefaultSize(videoWidth, widthMeasureSpec); 
        int height = getDefaultSize(videoHeight, heightMeasureSpec); 
        if (videoWidth > 0 && videoHeight > 0) { 
            if (videoWidth * height > width * videoHeight) { 
                height = width * videoHeight / videoWidth; 
            } else if (videoWidth * height < width * videoHeight) { 
                width = height * videoWidth / videoHeight; 
            } 
        } 
        Log.i(TAG, "setting size: " + width + "x" + height); 
        setMeasuredDimension(width, height); 
    } 
    @SuppressWarnings("deprecation")
	private void initVideoView() { 
        videoWidth = 0; 
        videoHeight = 0; 
        getHolder().addCallback(surfaceHolderCallback); 
        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        setFocusable(true); 
        setFocusableInTouchMode(true); 
        requestFocus(); 
    }
	/* (non-Javadoc)
	 * @see android.widget.MediaController.MediaPlayerControl#getAudioSessionId()
	 */
	public int getAudioSessionId() {
		// TODO Auto-generated method stub
		return 0;
	} 
	
	public int getModule_type() {
		return module_type;
	}

//	public void setVisibility(int visibility){
//		this.setVisibility(visibility);
//	}
	
	public void setModule_type(int module_type) {
		this.module_type = module_type;
	}

	public int getzOrder() {
		return zOrder;
	}

	public void setzOrder(int zOrder) {
		this.zOrder = zOrder;
	}

	public String getFile_version() {
		return file_version;
	}

	public void setFile_version(String file_version) {
		this.file_version = file_version;
	}

	public String getModule_name() {
		return module_name;
	}

	public void setModule_name(String module_name) {
		this.module_name = module_name;
		
	}

	public int getModule_uid() {
		return module_uid;
	}

	
	public void setModule_uid(int module_uid) {
		this.module_uid = module_uid;
	}

	public int getModule_gid() {
		return module_gid;
	}

	public void setModule_gid(int module_gid) {
		this.module_gid = module_gid;
	}
	
}
